package facade;

import interfaces.IFHotelController;

import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.List;

import model.Hospedagem;
import model.Hospede;
import model.Hotel;
import controller.HospedeController;
import controller.HotelController;
import converter.CPFConverter;
import dao.DAOHospedagem;
import dao.DAOHospede;

@SuppressWarnings("serial")
public class FachadaServicos extends UnicastRemoteObject implements
		IFHotelController {

	private HotelController hotelController;
	private HospedeController hospedeController;

	public FachadaServicos() throws RemoteException {
		hotelController = new HotelController();
		hospedeController = new HospedeController();
	}

	@Override
	public Hotel criarHotel(String nome, String gerente, Integer numeroDeQuartos)
			throws RemoteException {
		Hotel h = hotelController.criarHotel(nome, gerente, numeroDeQuartos);
		return h;
	}

	@Override
	public List<Hotel> getAllHoteis() throws RemoteException {
		return hotelController.getAllHoteis();
	}

	@Override
	public List<Hospede> getAllHospedes() throws RemoteException {
		return hospedeController.getAllHospedes();
	}

	@Override
	public Hospede criarHospede(String nome, String telefone, String endereco,
			String email, String cpf) throws RemoteException {
		
		Hospede h = hospedeController.criarHospede(nome, telefone, endereco,
				email, CPFConverter.getAsObject(cpf));
		return h;
	}
	
	@Override
	public Hospede findHospedeByCpf(String cpf) throws RemoteException {
		return new DAOHospede().findByCpf(CPFConverter.getAsObject(cpf));
	}
	
	/**
	 * Cria uma reserva.
	 * @param idHospede
	 * @param idHotel
	 * @param numQuarto
	 * @param dataChegada
	 * @param dataSaida
	 * @param isReserva
	 * @return
	 */
	@Override
	public Hospedagem criarHospedagem(Integer idHospede, Integer idHotel, int numQuarto, String dataChegada, String dataSaida, boolean isReserva) {
		Hospede hospede = hospedeController.checarPendencias(idHospede);
		if(hospede == null)
			return null;
		
		SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
		Calendar dataBegin = new GregorianCalendar();
		Calendar dataEnd = new GregorianCalendar();
		try {
			dataBegin.setTime(format.parse(dataChegada + " 12:00:00"));
			dataEnd.setTime(format.parse(dataSaida + " 11:59:59"));
		} catch (ParseException e) {
			e.printStackTrace();
			return null;
		}
		
		Hotel hotel = hotelController.isDisponivel(idHotel, numQuarto, dataBegin, dataEnd);
		if(hotel == null)
			return null;
			
		return hotelController.registrarReserva(hotel, hospede, numQuarto, dataBegin, dataEnd, isReserva);
	}
	
	public Boolean encerrarHospedagem(Integer idHospedagem) {
		if(new DAOHospedagem().findById(idHospedagem).isQuitada())
			return false;
		
		hotelController.encerrarHospedagem(idHospedagem);
		return true;
		
	}
	
	public Boolean cancelarReserva(int idHospedagem) throws RemoteException {
		if(!(new DAOHospedagem().findById(idHospedagem).isReserva()))
			return false;
		
		hotelController.cancelarReserva(idHospedagem);
		return true;
		
	}

	@Override
	public List<Hospedagem> getAllHospedagens() throws RemoteException {
		return new DAOHospedagem().findAll();
	}
	
	public Hospedagem getReserva(Hospede hospede) throws RemoteException {
		System.out.println(hospede.getId());
		for(Hospedagem hosp : new DAOHospedagem().findAllReservas()) {
			System.out.println(hosp.getId());
			if(hosp.getHospede().getId() == hospede.getId())
				return hosp;
		}
		
		return null;
	}

	@Override
	public List<Hospedagem> getAllReservas() throws RemoteException {
		return new DAOHospedagem().findAllReservas();
	}
	
	

}
